/***************************************************************************
 创建者：华磊
 开始时间：         : 2020.12.17
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   输出指令属性编辑视窗
*                                                                         *
 ***************************************************************************/
#include "outputinstructionview.h"
#include "ui_outputinstructionview.h"
#include "../../programView/programview.h"


OutputInstructionView::OutputInstructionView(int styleId,QWidget *parent):
    QWidget(parent),
    ui(new Ui::OutputInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);
}

void OutputInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:

        ui->groupBox->setGeometry(10,0,430,60);
        ui->DO_GBox->setGeometry(10,65,430,150);
        ui->AO_GBox->setGeometry(10,220,430,60);
        ui->note_GBox->setGeometry(10,285,430,65);
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void OutputInstructionView::updateView()
{
    ui->index_lineEdit->setText(index);

    ui->switch_radioButton->setEnabled(false);
    ui->pulse_radioButton->setEnabled(false);
    ui->register_radioButton->setEnabled(false);
    ui->switch_comboBox->setEnabled(false);
    ui->pulse_lineEdit->setEnabled(false);
    ui->registerNum_lineEdit->setEnabled(false);

    if(0 == outputType)
    {
        ui->DO_radioButton->setChecked(true);
        ui->switch_radioButton->setEnabled(true);
        ui->register_radioButton->setEnabled(true);
        ui->pulse_radioButton->setEnabled(true);
        ui->switch_comboBox->setEnabled(true);
        ui->pulse_lineEdit->setEnabled(true);
        ui->registerNum_lineEdit->setEnabled(true);
        ui->AO_lineEdit->setEnabled(false);

        if(EM_OUTPUT_TYPE_SWITCH == selectDoFlag)
        {
            ui->switch_radioButton->setChecked(true);
        }
        else if(EM_OUTPUT_TYPE_PULSE == selectDoFlag)
        {
            ui->pulse_radioButton->setChecked(true);
        }
        else if(EM_OUTPUT_TYPE_REGISTER == selectDoFlag)
        {
            ui->register_radioButton->setChecked(true);
        }
    }
    else if(1 == outputType)
    {
        ui->AO_radioButton->setChecked(true);
        ui->AO_lineEdit->setEnabled(true);
    }

    ui->switch_comboBox->setCurrentIndex(switchType);
    ui->pulse_lineEdit->setText(QString("%1").arg(pulseTime));
    ui->registerNum_lineEdit->setText(rIndex);
    ui->AO_lineEdit->setText(QString("%1").arg(aoValue));

    ui->note_lineEdit->setText(note);
}

void OutputInstructionView::getInput()
{
    index = ui->index_lineEdit->text();
    switchType = ui->switch_comboBox->currentIndex();
    pulseTime = ui->pulse_lineEdit->text().toFloat();
    rIndex = ui->registerNum_lineEdit->text();
    aoValue = ui->AO_lineEdit->text().toFloat();
    note = ui->note_lineEdit->text();
}

void OutputInstructionView::on_switch_radioButton_clicked()
{
    emit selectSwitch();
}

void OutputInstructionView::on_pulse_radioButton_clicked()
{
    emit selectPulse();
}

void OutputInstructionView::on_register_radioButton_clicked()
{
    emit selectRegister();
}

void OutputInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit clearNote();
}

void OutputInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void OutputInstructionView::on_save_Btn_clicked()
{
    getInput();

    emit finishEditInstruction();
    emit saveInstruction(outputInstruction);
}

void OutputInstructionView::on_DO_radioButton_clicked()
{
    emit selectDOInstruction();
}

void OutputInstructionView::on_AO_radioButton_clicked()
{
    emit selectAOInstruction();
}

void OutputInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/outViewEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}
